<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 具体页面结构 -->
        <el-tabs>
          <!-- 放置页签 -->
          <el-tab-pane label="角色管理">
            <!-- 新增角色按钮 -->
            <el-row style="height:60px">
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"

                @click="xinzheng"
              >新增角色</el-button>
            </el-row>
            <!-- 表格 -->
            <el-table :data="list">
              <el-table-column label="序号" width="120" type="index" />
              <el-table-column label="角色名称" prop="name" width="240" />
              <el-table-column label="描述" prop="description" />
              <el-table-column label="操作">
                <template slot-scope="scope">
                  <el-button size="small" type="success" @click="fenpei(scope.row.id)">分配权限</el-button>
                  <el-button size="small" type="primary" @click="bianji(scope.row)">编辑</el-button>
                  <el-button size="small" type="danger" @click="shanchu(scope.row.id)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>

            <el-row type="flex" justify="center" align="middle" style="height: 60px">
              <!-- 分页组件 -->
              <el-pagination background :total="total" layout="prev,pager,next" :page-size="pageParams.pagesize" @current-change="hCurrentChange" />
            </el-row>
          </el-tab-pane>
        </el-tabs>

        <!-- 分配权限的弹层 -->
        <el-dialog
          title="分配权限(一级为路由页面查看权限-二级为按钮操作权限)"
          :visible.sync="show"
          :close-on-click-modal="false"
          :close-on-press-escape="false"
        >
          <AssignPermission v-if="show" :id="id" @close="close" />
        </el-dialog>
      </el-card>
      <!-- 新增弹框 -->
      <el-dialog
        :title="title"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        :visible.sync="showDialog"
        @close="closeForm"
      >
        <el-form ref="roleForm" :model="roleForm" :rules="rules" label-width="100px">
          <el-form-item label="角色名称" prop="name">
            <el-input v-model="roleForm.name" />
          </el-form-item>
          <el-form-item label="角色描述">
            <el-input v-model="roleForm.description" />
          </el-form-item>
        </el-form>
        <!-- 底部 -->
        <el-row slot="footer" type="flex" justify="center">
          <el-col :span="6">
            <el-button size="small" @click="quxiao">取消</el-button>
            <el-button size="small" type="primary" @click="tijiao">确定</el-button>
          </el-col>
        </el-row>
      </el-dialog>

    </div>

  </div>
</template>
<script>
import { getRoles, deleteRole, addRole, updateRole } from '@/api/setting'
import AssignPermission from './assignPermission'
export default {
  components: {
    AssignPermission
  },
  data() {
    return {
      list: [], // 表格数据
      pageParams: {
        page: 1, // 1页
        pagesize: 3 // 行数
      },
      isid: null, // id删除的ID
      total: 0, // 默认为0
      showDialog: false, // 默认不显示
      roleForm: { // 请求数据参数// 顺便数据回填
        name: '', // 角色名称
        description: ''// 角色描述
      },
      // 表单验证规则
      rules: {
        name: [
          { required: true, message: '角色名称不能为空', trigger: 'blur' }
        ]

      },
      title: '', // 新增编辑控制
      isEdit: false, // 是否是编辑
      show: false, // 分配权限显示
      id: ''// 传给子组件ID
    }
  },

  created() {
    this.loadRoles()// 获取表格数据
  },
  methods: {
    // 获取表格数据
    async loadRoles() {
      try {
        const res = await getRoles(this.pageParams)
        console.log('表格数据1', res)
        this.list = res.data.rows
        console.log(this.list)
        this.total = res.data.total
      } catch (err) {
        console.log(err)
      }
    },
    hCurrentChange(curPage) {
      // 当用户点击页码的时候我需要一个参数来确定点的是那个
      // 更新页码
      this.pageParams.page = curPage
      // 重新发送请求
      this.loadRoles()
    },
    async doDel() { // 删除数据
      // console.log()
      try {
        const res = await deleteRole(this.isid)
        console.log('删除', res)
        this.$message.success('删除成功')
        if (this.list.length === 1) {
          this.pageParams.page--
        }
        if (this.pageParams.page <= 0) {
          this.pageParams.page = 1
        }
        this.loadRoles()// 重新获取数据
      } catch (err) {
        console.log(err)
        this.$message.success('删除失败')
      }
    },
    async doAdd() { // 添加数据
      try {
        const res = await addRole(this.roleForm)
        console.log('添加角色', res)
        this.title = '新增数据'
        this.$message.success('添加成功')
        this.showDialog = false
        this.loadRoles()
        // this.roleForm = {}// 清空内容
      } catch (err) {
        this.$message({
          showClose: true,
          message: '请填写描述',
          type: 'error'
        })
        this.roleForm = {}// 清空内容
      }
    },
    async doEdit() { // 编辑数据
      try {
        const res = await updateRole(this.roleForm)
        console.log(res)
        this.$message.success('编辑成功')

        this.loadRoles()// 发送获取表格数据刷新
        this.showDialog = false
        // this.roleForm = {}// 清空内容
      } catch (err) {
        console.log(err)
        this.$message({
          showClose: true,
          message: '编辑失败',
          type: 'error'
        })
        this.roleForm = {}// 清空内容
      }
    },
    shanchu(id) { // 删除数据id
      this.isid = id// id传给变量ID
      this.$confirm('此操作将永久删除该角色, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.doDel()// 点击确定则删除数据
      }).catch((err) => {
        console.log(err)
      })
    },
    xinzheng() { // 新增数据
      this.showDialog = true
      this.isEdit = false// 区分他们，false是新增
      if (this.isEdit === false) {
        this.title = '新增数据'
      }
    },
    bianji(row) { // 编辑数据
      this.isEdit = true// 区分他们，true是编辑
      this.showDialog = true
      // this.roleForm = row// 将当前的数据直接给表单
      this.roleForm = { id: row.id, name: row.name, description: row.description }
      if (this.isEdit === true) {
        this.title = '编辑数据'
      }
    },
    tijiao() { // 提交按钮
      this.$refs.roleForm.validate(valid => {
        console.log(valid)
        if (valid) {
          if (this.isEdit) {
            this.doEdit()// 发送编辑请求
            this.roleForm = {}
          } else {
            this.doAdd()// 发送添加数据请求
          }
        }
      })

      // console.log(this.roleForm)
    },
    quxiao() { // 取消按钮
      this.showDialog = false
    },
    closeForm() { // 重置表单
      this.roleForm = {}
      this.$refs.roleForm.resetFields()// 重置表单
    },
    fenpei(id) {
      this.show = true// 控制分配权限弹窗显示
      this.id = id// 给子组件把id传过去
    },
    close() {
      this.show = false
    }
  }
}
</script>
